package data

import (
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

type LogisticsRepo struct{}

type TransportDto struct {
	Id        int     `orm:"pk;auto;column(id)"`
	WaybillId int     `orm:"column(waybill_id)"`
	Lng       float32 `orm:"column(lng)"`
	Lat       float32 `orm:"column(lat)"`
	Time      string  `orm:"column(create_time)"`
}

func init() {
	orm.RegisterModel(new(TransportDto))
}

func (c *TransportDto) TableName() string {
	return "l_transport"
}

func (l LogisticsRepo) GetAllAreas() []orm.ParamsList {
	var lists []orm.ParamsList
	o := orm.NewOrm()
	_, _ = o.Raw(SQL_Logis_Get_Area).ValuesList(&lists)
	return lists
}

func (l LogisticsRepo) GetAreaID(name string) string {
	var id string
	var list []orm.ParamsList
	o := orm.NewOrm()
	_, _ = o.Raw(SQL_Logis_Get_Area + " WHERE name LIKE '%" + name + "%';").ValuesList(&list)
	if len(list) > 0 {
		id = list[0][0].(string)
	} else {
		id = "110000"
	}
	return id
}

func (l LogisticsRepo) AddTransportLog(list []TransportDto) error {
	o := orm.NewOrm()
	_, err := o.InsertMulti(50, list)
	return err
}

func (l LogisticsRepo) GetLastTransportLog(waybillId int) (dto TransportDto, err error) {
	o := orm.NewOrm()
	err = o.Raw(SQL_Logis_Get_Last_Transport_Log, waybillId).QueryRow(&dto)
	return
}

func (l LogisticsRepo) GetTransportLogs(waybillId, offset int) (list []TransportDto, err error) {
	o := orm.NewOrm()
	_, err = o.Raw(SQL_Logis_Get_Transport_Logs, waybillId, offset).QueryRows(&list)
	return
}
